package io.zhiller.simpletemplate.api.git_repository

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import io.ktor.client.call.body
import io.ktor.client.request.get
import io.ktor.client.statement.HttpResponse

import io.ktor.http.ContentType
import io.ktor.http.contentType
import io.zhiller.simpletemplate.api.httpClient


class GitApis : GitApiDefines {
  
  companion object {
    // 如果你使用gitee仓库，请将ACCESS_KEY设置为gitee申请的私钥
    const val ACCESS_TOKEN_NAME = "compose_sample"
    const val ACCESS_TOKEN = "ccb69867f6e2479a5bad18912b98ea5b"
    const val API_PREFIX = "https://gitee.com/api/v5"
  }
  
  override suspend fun getAllReleases(
    owner: String, repo: String, page: Int, perPage: Int
  ): List<ReleaseDTO> {
    val url = "$API_PREFIX/repos/$owner/$repo/releases"
    val parameters = buildString {
      append("?access_token=$ACCESS_TOKEN")
      append("&page=$page")
      append("&per_page=$perPage")
    }
    val fullUrl = "$url$parameters"
    return try {
      val response: HttpResponse = httpClient.get(fullUrl) {
        contentType(ContentType.Application.Json)
      }
      val responseBody = response.body<String>()
      val mapper = jacksonObjectMapper()
      mapper.readValue<List<ReleaseDTO>>(responseBody)
    } finally {
    }
  }

//  const val OWNER = "kaajjo"
//  const val REPO = "LibreSudoku"
//  const val REPO_URL = "https://github.com/kaajjo/LibreSudoku"
}